package com.hoow.shakoow.shake.adapter

import android.annotation.SuppressLint
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.RelativeLayout
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.hoow.shakoow.R
import com.hoow.shakoow.ai.data.RevObjUserAblum
import com.hoow.shakoow.ai.utils.DisplayUtils
import com.hoow.shakoow.extend.isValidLink

class UserAblumAdapter(
    private val isSelfMode: Boolean,
    private val dataList: List<RevObjUserAblum>
) :
    RecyclerView.Adapter<UserAblumAdapter.ViewHolder>() {

    companion object {
        //列值宽度
        var mItemWidth: Int = 0

        //列值高度
        var mItemHeight: Int = 0

        // 增加
        const val ACTION_ADD = 0

        // 点击
        const val ACTION_CLICK = 1

        // 删除选中
        const val ACTION_DELETE_SELECTED = 2

        /**
         * 初始化列值宽与高
         *
         * @param context 上下文
         */
        fun initItemSize(context: Context) {
            val screenWidth = DisplayUtils.getScreenWidth(context) - 12
            mItemWidth = screenWidth / 4
            mItemHeight = mItemWidth//(mItemWidth * (360F / 250)).toInt()
        }
    }

    /**
     * 点击回调
     *
     * @return Int 位置
     * @return RevObjAiHomeDemo 值
     * */
    private var mClickActionListener: (Int, RevObjUserAblum, Int) -> Unit =
        { _, _, _ -> }

    inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        var rlAblumContentBg: RelativeLayout = view.findViewById(R.id.rlAblumContentBg)
        var imageAdapterAblum: ImageView = view.findViewById(R.id.imageAdapterAblum)
        var imageAdapterAblumAdd: ImageView = view.findViewById(R.id.imageAdapterAblumAdd)
        var llAdapterAblumAddBg: LinearLayout = view.findViewById(R.id.llAdapterAblumAddBg)
        var rlAdapterAblumDeleteBg: RelativeLayout = view.findViewById(R.id.rlAdapterAblumDeleteBg)
        var vAdapterAblumDeleteMask: View = view.findViewById(R.id.vAdapterAblumDeleteMask)
        var imageAdapterAblumDeleteSelect: ImageView =
            view.findViewById(R.id.imageAdapterAblumDeleteSelect)
    }

    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int,
    ): ViewHolder {
        val view = LayoutInflater.from(parent.context)
            .inflate(R.layout.view_adpter_user_ablum, parent, false)
        return ViewHolder(view)
    }

    @SuppressLint("SetTextI18n")
    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val data: RevObjUserAblum = dataList[position]

        //更新宽高
        if ((mItemWidth > 0) and (mItemHeight > 0)) {
            val layoutParams: ViewGroup.LayoutParams = holder.rlAblumContentBg.layoutParams
            layoutParams.width = mItemWidth
            layoutParams.height = mItemHeight
            holder.rlAblumContentBg.layoutParams = layoutParams
        }

        // 设置删除项
        if (isSelfMode and (position == 0)) {
            holder.imageAdapterAblum.visibility = View.GONE
            holder.llAdapterAblumAddBg.visibility = View.VISIBLE
        } else {
            holder.imageAdapterAblum.visibility = View.VISIBLE
            holder.llAdapterAblumAddBg.visibility = View.GONE
        }

        // 设置编辑状态
        if (isSelfMode and (position > 0) and data.isEdit) {
            holder.rlAdapterAblumDeleteBg.visibility = View.VISIBLE

            // 设置选中状态
            if (data.isSelected) {
                holder.vAdapterAblumDeleteMask.setBackgroundResource(R.drawable.bg_ablum_delete_mask_selected)
                holder.imageAdapterAblumDeleteSelect.setImageResource(R.drawable.checkbox_yes_large)

            } else {
                holder.vAdapterAblumDeleteMask.setBackgroundResource(R.drawable.bg_ablum_delete_mask_unselect)
                holder.imageAdapterAblumDeleteSelect.setImageResource(R.drawable.checkbox_no_large)
            }
        } else {
            holder.rlAdapterAblumDeleteBg.visibility = View.GONE
        }

        //设置图像
        data.imgUrl?.let {
            if (it.isValidLink()) {
                Glide.with(holder.imageAdapterAblum)
                    .load(it)
                    .into(holder.imageAdapterAblum)
            }
        }

        if (data.imgUrl == null) {
            holder.imageAdapterAblum.setImageResource(R.drawable.icon_placeholder_large)
        }

        // 设置回调
        holder.rlAdapterAblumDeleteBg.setOnClickListener {
            mClickActionListener.invoke(position, data, ACTION_DELETE_SELECTED)
        }

        //设置回调
        holder.llAdapterAblumAddBg.setOnClickListener {
            mClickActionListener.invoke(position, data, ACTION_ADD)
        }

        //设置回调
        holder.imageAdapterAblumAdd.setOnClickListener {
            mClickActionListener.invoke(position, data, ACTION_ADD)
        }

        //设置回调
        holder.imageAdapterAblum.setOnClickListener {
            mClickActionListener.invoke(position, data, ACTION_CLICK)
        }
    }

    override fun getItemCount(): Int {
        return dataList.size
    }

    /**
     * 设置点击回调
     */
    fun setItemClickListener(listener: (Int, RevObjUserAblum, Int) -> Unit) {
        mClickActionListener = listener
    }
}